﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Bussiness;
using HTTT.Utilities;
using HTTT.DataAccess;
using HTTT.AD;
using System.Configuration;

namespace HTTT.admin.adgroup
{
    public partial class GroupList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["group_keyword"] != null)
                {
                    txtKeyword.Text = Session["group_keyword"].ToString();
                }
                CheckPermission();
                LoadData();
            }
        }

        private void CheckPermission()
        {
            if (!Page.User.IsInRole(Constants.ROLE_SUPER_ADMINISTRATOR)
                 && !Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR))
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadData()
        {
            string selectedValue = "";
            if (Session["group_oupath"] != null)
            {
                selectedValue = Session["group_oupath"].ToString();
            }
            //Bind OU list
            LoadOU(selectedValue);
            if (txtKeyword.Text != string.Empty || ddlOU.SelectedIndex != 0)
            {
                DoSearch();
            }
        }

        private void LoadOU(string selectedValue)
        {
            //Get OU list
            HT_OUBO Bo = new HT_OUBO();
            List<HT_OU> ListBO = Bo.GetAllHT_OU().ToList();
            ddlOU.DataSource = ListBO;
            ddlOU.DataValueField = ADOUProperties.DISTINGUISHEDNAME;
            ddlOU.DataTextField = ADOUProperties.NAME;
            ddlOU.DataBind();
            ddlOU.Items.Insert(0, new ListItem("▼ " + ConfigurationManager.AppSettings["LDAPDomain"], ConfigurationManager.AppSettings["DomainPath"]));
            ddlOU.SelectedValue = selectedValue;
        }

        protected void btnModifyMember_Click(Object sender, EventArgs e)
        {
            string ID = GetSelectedID();
            if (ID == string.Empty)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 group"), true);
                return;
            }
            Response.Redirect("ModifyMemberGroup.aspx?ID=" + ID);
        }
        protected void btnAdd_Click(Object sender, EventArgs e)
        {
            Response.Redirect("AddGroup.aspx");
        }
        protected void btnEdit_Click(Object sender, EventArgs e)
        {
            string ID = GetSelectedID();
            if (ID == string.Empty)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 group"), true);
                return;
            }
            Response.Redirect("EditGroup.aspx?ID=" + ID);
        }
        private string GetSelectedID()
        {
            string SelectedID = "";
            foreach (RepeaterItem ri in rptData.Items)
            {
                CheckBox chk = (CheckBox)ri.FindControl("chkItem");
                if (chk.Checked)
                {
                    SelectedID = chk.Attributes["value"];
                }
            }
            return SelectedID;
        }

        protected void btnDelete_Click(Object sender, EventArgs e)
        {
            string ID = GetSelectedID();
            if (ID == string.Empty)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 group"), true);
                return;
            }
            Response.Redirect("DeleteGroup.aspx?ID=" + ID);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DoSearch();
        }

        private void DoSearch()
        {
            string KeyWord = txtKeyword.Text.Trim();
            string OUPath = ddlOU.SelectedValue;
            Session["group_keyword"] = KeyWord;
            Session["group_oupath"] = OUPath;
            HT_GroupsBO BO = new HT_GroupsBO();
            IEnumerable<HT_Groups> ListData = BO.SearchGroup(KeyWord, OUPath);
            if (ListData == null)
            {
                lblMsg.Text = "Không tìm thấy kết quả !";
                return;
            }
            rptData.DataSource = ListData;
            rptData.DataBind();
        }


        protected void btnClear_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}